package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrdersService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 提交订单
     *
     * @param orders
     * @return
     */
    @PostMapping("submit")
    public R submit(@RequestBody Orders orders) {
        orderService.submit(orders);
        return R.success("提交成功");
    }

    /**
     * 前台订单分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("userPage")
    public R userPage(Integer page, Integer pageSize) {
        Long userId = BaseContext.getThreadLocal();
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.lambdaQuery(Orders.class)
                .eq(Orders::getUserId, userId);
        orderService.page(ordersPage, queryWrapper);

        Page<OrdersDto> orderDetailPage = new Page<>();
        List<OrdersDto> detailList = new ArrayList<>();
        BeanUtils.copyProperties(ordersPage, orderDetailPage, "records");
        for (Orders orders : ordersPage.getRecords()) {
            OrdersDto dto = new OrdersDto();
            BeanUtils.copyProperties(orders, dto);
            LambdaQueryWrapper<OrderDetail> wrapper = Wrappers.lambdaQuery(OrderDetail.class)
                    .eq(OrderDetail::getOrderId, orders.getId());
            List<OrderDetail> list = orderDetailService.list(wrapper);
            dto.setOrderDetails(list);
            detailList.add(dto);
        }
        orderDetailPage.setRecords(detailList);

        return R.success(orderDetailPage);
    }

    @GetMapping("page")
    public R page(Integer page, Integer pageSize, Integer number,
                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime,
                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.lambdaQuery(Orders.class)
                .eq(number != null, Orders::getNumber, number)
                //.between(Orders::getOrderTime, beginTime, endTime);
                .gt(!Objects.isNull(beginTime), Orders::getOrderTime, beginTime)
                .lt(!Objects.isNull(endTime), Orders::getOrderTime, endTime);
        orderService.page(ordersPage, queryWrapper);

        return R.success(ordersPage);

    }
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        //调用方法修改
        orderService.updateById(orders);
        return R.success("修改成功");
    }

}
